//
//  CMJTabbarController.m
//  CMJWeibo
//
//  Created by BH on 15/12/4.
//
//

#import "CMJTabbarController.h"
#import "CMJNavgationController.h"
#import "HomeController.h"
#import "MessageController.h"
#import "ComposeController.h"
#import "SearchController.h"
#import "AboutMeController.h"
#import "CMJTabbar.h"
#import "UIImage+MJ.h"
#import "CMJCommon.h"
#import "UnreadCountParam.h"
#import "UserTool.h"
#import "AccountTool.h"

@interface CMJTabbarController ()<CMJTabbarDelegate>

@property(nonatomic,weak)CMJTabbar *tabbar;
@property(nonatomic,strong)HomeController *homeCtrl;
@property(nonatomic,strong)MessageController *messageCtrl;
@property(nonatomic,strong)SearchController *searchCtrl;
@property(nonatomic,strong)AboutMeController *aboutMeCtrl;

@end

@implementation CMJTabbarController

#pragma mark - 初始化方法
-(CMJTabbar *)tabbar
{
    if (!_tabbar) {
        CMJTabbar *customTabbar = [[CMJTabbar alloc] init];
        customTabbar.frame = self.tabBar.bounds;
        customTabbar.delegate = self;
        [self.tabBar addSubview:customTabbar];
        self.tabbar = customTabbar;
    }
    return _tabbar;
}

#pragma mark init方法内部会调用这个
-(instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        
        // 初始化自己的所有子控制器
        HomeController *home = [[HomeController alloc] init];
        [self setupChildVC:home title:@"首页" imageName:@"tabbar_home" selectedImageName:@"tabbar_home_selected"];
        self.homeCtrl = home;
        
        MessageController *msg = [[MessageController alloc] init];
        [self setupChildVC:msg title:@"消息" imageName:@"tabbar_message_center" selectedImageName:@"tabbar_message_center_selected"];
        self.messageCtrl = msg;
        
        SearchController *search = [[SearchController alloc] init];
        [self setupChildVC:search title:@"发现" imageName:@"tabbar_discover" selectedImageName:@"tabbar_discover_selected"];
        self.searchCtrl = search;
        
        AboutMeController *about = [[AboutMeController alloc] init];
        [self setupChildVC:about title:@"我" imageName:@"tabbar_profile" selectedImageName:@"tabbar_profile_selected"];
        self.aboutMeCtrl = about;
        
    }
    return self;
}

#pragma mark 初始化一个子控制器
/**
 *  初始化一个子控制器
 *
 *  @param child             需要初始化的子控制器
 *  @param title             标题
 *  @param imageName         图标
 *  @param selectedImageName 选中的图标
 */
- (void)setupChildVC:(UIViewController *)child title:(NSString *)title imageName:(NSString *)imageName selectedImageName:(NSString *)selectedImageName
{
    //设置标题
    child.title = title;
    
    //设置图片
    child.tabBarItem.image = [UIImage imageWithName:imageName];
    UIImage *selectImg = [UIImage imageWithName:selectedImageName];
    if (iOS7) {
        // 如果是iOS7, 不需要渲染图片
        selectImg = [selectImg imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    }
    child.tabBarItem.selectedImage = selectImg;
    
    //添加子控制器
    CMJNavgationController *navCtrl = [[CMJNavgationController alloc] initWithRootViewController:child];
    [self addChildViewController:navCtrl];
    
    //添加选项卡按钮
    [self.tabbar addTabbarItem:child.tabBarItem];
}

#pragma mark view加载完毕
-(void)viewDidLoad
{
    [super viewDidLoad];
    
    // 定时发送请求给服务器(获得未读数)
    NSTimer *timer = [NSTimer scheduledTimerWithTimeInterval:15.0 target:self selector:@selector(getUnreadCount) userInfo:nil repeats:YES];
    [[NSRunLoop mainRunLoop] addTimer:timer forMode:NSRunLoopCommonModes];
}

/**
 *  获取未读消息数
 */
-(void)getUnreadCount
{
    // 封装请求参数
    UnreadCountParam *param = [UnreadCountParam param];
    param.uid = [AccountTool account].uid;
    
    // 2.获得未读数
    [UserTool getUnreadCountWithParam:param success:^(UnreadCountResult *result) {
        
        self.homeCtrl.tabBarItem.badgeValue = [NSString stringWithFormat:@"%d",result.status];
        [UIApplication sharedApplication].applicationIconBadgeNumber = result.status;
        
    } failure:^(NSError *error) {
        NSLog(@"%@",error.localizedDescription);
    }];
}

-(void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    
    // 移除系统自动产生的UITabBarButton
    for (UIView *childView in self.tabBar.subviews) {
        if ([childView isKindOfClass:[UIControl class]]) {
            [childView removeFromSuperview];
        }
    }
}

#pragma mark -CMJTabBarDelegate
-(void)tabbar:(CMJTabbar *)tabbar didSelectButtonFrom:(NSInteger)from to:(NSInteger)to
{
    if (to == 0) {
        //刷新首页
        [self.homeCtrl refreshFromOther:from != 0];
    }
    self.selectedIndex = to;
}

-(void)tabbarDidClickPlusButton:(CMJTabbar *)tabbar
{
    ComposeController *composeCtrl = [[ComposeController alloc] init];
    composeCtrl.type = ComposeTypeBlog;
    CMJNavgationController *navCtrl = [[CMJNavgationController alloc] initWithRootViewController:composeCtrl];
    [self presentViewController:navCtrl animated:YES completion:nil];
}

@end
